import axios from "axios";
import { Toast } from "vant";
axios.defaults.timeout = 90000;
// axios.defaults.baseURL = 'http://192.168.3.243'
axios.defaults.baseURL = 'http://api.mimaapp.com'
// axios.defaults.baseURL = 'http://api.guangguangapp.com'
axios.defaults.headers={Device:1}  //添加自定义请求头  处理webp格式 图片
// 过滤器
axios.interceptors.request.use(
	(config) => {
		let token = $cookies.get('Token')  || '' 
		token && (config.headers.Authorization = token)
		return config;
	},
	(err) => {
		return Promise.reject(err);
	}
);
// 拦截器
axios.interceptors.response.use(
	(response) => {
		if (response.headers.authorization) {
			$cookies.set("Token", response.headers.authorization, -1)
		}
		let { code } = response.data;
		if (code === 0 || code === 66000) {
			return Promise.resolve(response);
		} else {
			if (code === 50000 || code === 50107 || code === 50108 || code === 50109 || code === 50113) {
				$cookies.remove('Token')
			} else {
				Toast(response.data.msg)
			}
			return Promise.reject(response);
		}
	},
	
);

/** 
 * get方法，对应get请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function get(url, params){    
    return new Promise((resolve, reject) =>{        
        axios.get(url, {            
            params: params        
        })        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            reject(err.data)        
        })    
    });
}


/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {    
    return new Promise((resolve, reject) => {         
        axios.post(url, params)        
        .then(res => {            
            resolve(res.data);        
        })        
        .catch(err => {            
            reject(err.data)        
        })    
    });
}
